package com.apollow.oschina.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.apollow.oschina.R;
import com.apollow.oschina.base.Global;
import com.apollow.oschina.bean.TweetBean;
import com.apollow.oschina.ui.TweetDetailActivity;
import com.apollow.oschina.util.PicassoUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2017/1/18.
 * 项目名:    OsChina
 * 包名:      com.apollow.oschina.adapter
 * 文件名:    NewTweetAdapter
 * 创建者:    LXF
 * 创建时间:   ${DATA}22:39
 * 描述:     TODO
 */

public class NewTweetAdapter extends BaseAdapter {
    public List<TweetBean> tweets = new ArrayList<>();

    public Activity mActivity;

    public NewTweetAdapter(Activity activity) {
        mActivity = activity;
    }

    public void setTweets(List<TweetBean> tweets) {
        this.tweets = tweets;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return tweets == null ? 0 : tweets.size();
    }

    @Override
    public Object getItem(int position) {
        if (tweets != null) {
            return tweets.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder hold = null;
        if (convertView == null) {
            convertView = Global.inflate(R.layout.item_tweet_detail, parent);
            hold = new ViewHolder(convertView);
            convertView.setTag(hold);
        } else {
            hold = (ViewHolder) convertView.getTag();
        }
        hold.setData(tweets.get(position));
        return convertView;
    }
    public class ViewHolder {
        @BindView(R.id.iv_user)
        CircleImageView mIvUser;
        @BindView(R.id.tv_name)
        TextView mTvName;
        @BindView(R.id.tv_desc)
        TextView mTvDesc;
        @BindView(R.id.tv_data_synthesize)
        TextView mTvDataSynthesize;
        @BindView(R.id.tv_discuss_count)
        TextView mTvDiscussCount;
        TweetBean tweetBean;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(mActivity, TweetDetailActivity.class);
                    intent.putExtra("UID",tweetBean.getId());
                    mActivity.startActivity(intent);
                }
            });
        }
        public void setData(TweetBean tweetBean) {
            this.tweetBean=tweetBean;
            PicassoUtil.displayImage(mActivity, tweetBean.getPortrait(),R.drawable.userimage,mIvUser);
            mTvName.setText(tweetBean.getAuthor());
            mTvDesc.setText(tweetBean.getBody());
            mTvDataSynthesize.setText(tweetBean.getPubDate());
            mTvDiscussCount.setText(tweetBean.getCommentCount()+"");
        }
    }
}
